home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Draw / Sources / GroupCmd.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  11.1 KB  |  331 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                GroupCmd.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef GROUPCMD_H
  15. #include "GroupCmd.h"
  16. #endif
  17.  
  18. #ifndef DRAWCONT_H
  19. #include "DrawCont.h"
  20. #endif
  21.  
  22. #ifndef DRAWPART_H
  23. #include "DrawPart.h"
  24. #endif
  25.  
  26. #ifndef DRAWSEL_H
  27. #include "DrawSel.h"
  28. #endif
  29.  
  30. #ifndef BASESHP_H
  31. #include "BaseShp.h"
  32. #endif
  33.  
  34. #ifndef GROUPSHP_H
  35. #include "GroupShp.h"
  36. #endif
  37.  
  38. // for cGroup
  39. #ifndef DEFINES_K
  40. #include "Defines.k"
  41. #endif
  42.  
  43. // ----- FrameWork Includes -----
  44.  
  45. #ifndef FWPRESEN_H
  46. #include "FWPresen.h"
  47. #endif
  48.  
  49. #ifndef SOM_ODShape_xh
  50. #include <Shape.xh>
  51. #endif
  52.  
  53. #ifdef __MRC__
  54. // This is to go around a bug in MRC. It doesn't seems to see the extern
  55. // in BaseShp.cpp
  56. FW_DEFINE_AUTO_TEMPLATE(FW_TRefCountedCollection, CBaseShape)
  57. #endif
  58.  
  59. //========================================================================================
  60. // Segmentation
  61. //========================================================================================
  62.  
  63. #ifdef FW_BUILD_MAC
  64. #pragma segment odfdrawcommand
  65. #endif
  66.  
  67. //========================================================================================
  68. // class CGroupContent
  69. //========================================================================================
  70.  
  71. FW_DEFINE_AUTO(CGroupContent)
  72.  
  73. //----------------------------------------------------------------------------------------
  74. //    CGroupContent constructor
  75. //----------------------------------------------------------------------------------------
  76. CGroupContent::CGroupContent(Environment* ev, CDrawPart* drawPart, CDrawContent* other)
  77. :    CDrawContent(ev, drawPart, other)
  78. {
  79.     FW_END_CONSTRUCTOR    
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    CGroupContent destructor
  84. //----------------------------------------------------------------------------------------
  85. CGroupContent::~CGroupContent()
  86. {
  87.     FW_START_DESTRUCTOR
  88. }
  89.  
  90. //----------------------------------------------------------------------------------------
  91. //    CGroupContent::GetShapeList
  92. //----------------------------------------------------------------------------------------
  93. CShapeCollection* CGroupContent::GetShapeList()
  94. {
  95.     return fShapeList;
  96. }
  97.  
  98. #pragma segment Main
  99.  
  100.  
  101. //========================================================================================
  102. // CGroupShapesCommand
  103. //========================================================================================
  104.  
  105. FW_DEFINE_AUTO(CGroupShapesCommand)
  106.  
  107. //----------------------------------------------------------------------------------------
  108. //    CGroupShapesCommand constructor
  109. //----------------------------------------------------------------------------------------
  110. CGroupShapesCommand::CGroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
  111. :    FW_CCommand(ev, cGroup, frame, FW_kCanUndo),
  112.     fDrawSelection(selection),
  113.     fGroupShape(NULL),
  114.     fGroupedContent(NULL)
  115. {
  116.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoGroupMsg, kRedoGroupMsg);
  117.     FW_END_CONSTRUCTOR    
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. //    CGroupShapesCommand destructor
  122. //----------------------------------------------------------------------------------------
  123. CGroupShapesCommand::~CGroupShapesCommand()
  124. {
  125.     FW_START_DESTRUCTOR
  126.     
  127.     delete fGroupedContent;
  128.     fGroupShape->Release();
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. //    CGroupShapesCommand::DoIt
  133. //----------------------------------------------------------------------------------------
  134. void CGroupShapesCommand::DoIt(Environment* ev)
  135. {
  136.     // Save Undo state - copy selected shapes
  137.     fGroupedContent = FW_NEW(CGroupContent, (ev, fDrawSelection->GetDrawPart(), fDrawSelection->GetDrawContent(ev)));
  138.  
  139.     // Combine selected shapes into a new group shape
  140.     CShapeCollection* shapeList = fGroupedContent->GetShapeList();
  141.     fGroupShape = FW_NEW(CGroupShape, ((CDrawPart*) fDrawSelection->GetPart(ev), shapeList));
  142.  
  143.     this->GroupShapes(ev);
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. //    CGroupShapesCommand::UndoIt
  148. //----------------------------------------------------------------------------------------
  149. void CGroupShapesCommand::UndoIt(Environment* ev)
  150. {
  151.     // Ungroup the shapes
  152.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  153.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  154.  
  155.     // Add each shape back into the part's list
  156.     CDrawContentShapeIterator it(fGroupedContent);
  157.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  158.     {
  159.         partContent->AddShape(ev, shape, fGroupShape);    // insert before the group shape
  160.         shape->SetGroupedState(NULL);
  161.     }
  162.  
  163.     // Remove group shape from the part
  164.     partContent->RemoveShape(ev, fGroupShape);
  165.  
  166.     // Select the shapes
  167.     fDrawSelection->SelectContent(ev, fGroupedContent);
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. //    CGroupShapesCommand::RedoIt
  172. //----------------------------------------------------------------------------------------
  173. void CGroupShapesCommand::RedoIt(Environment* ev)
  174. {
  175.     this->GroupShapes(ev);
  176. }
  177.  
  178. //----------------------------------------------------------------------------------------
  179. //    CGroupShapesCommand::GroupShapes
  180. //----------------------------------------------------------------------------------------
  181. void CGroupShapesCommand::GroupShapes(Environment* ev)
  182. {
  183.     // Remove shapes from the selection
  184.     fDrawSelection->CloseSelection(ev);
  185.  
  186.     // Remove each shape from the part's list (but don't detach it)
  187.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  188.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  189.     
  190.     CDrawContentShapeIterator it(fGroupedContent);
  191.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  192.     {
  193.         partContent->RemoveShape(ev, shape);
  194.         shape->SetGroupedState(fGroupShape);
  195.     }
  196.  
  197.     // Add the new group shape to the part and select it
  198.     drawPart->AddShapeToPart(ev, fGroupShape);
  199.     fDrawSelection->AddToSelection(ev, fGroupShape, true);
  200. }
  201.  
  202. //========================================================================================
  203. // CUngroupShapesCommand
  204. //========================================================================================
  205.  
  206. FW_DEFINE_AUTO(CUngroupShapesCommand)
  207.     
  208. //----------------------------------------------------------------------------------------
  209. //    CUngroupShapesCommand constructor
  210. //----------------------------------------------------------------------------------------
  211. CUngroupShapesCommand::CUngroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
  212. :    FW_CCommand(ev, cUngroup, frame, FW_kCanUndo),
  213.     fDrawSelection(selection),
  214.     fGroupList(NULL)
  215. {
  216.     // Copy selected shapes that are group shapes
  217.     fGroupList = FW_NEW(CShapeCollection, ());
  218.     CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
  219.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  220.     {
  221.         if (shape->GetShapeType() == kGroupShape)
  222.             fGroupList->AddLast(shape);
  223.     }
  224.  
  225.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoUngroupMsg, kRedoUngroupMsg);
  226.     FW_END_CONSTRUCTOR    
  227. }
  228.  
  229. //----------------------------------------------------------------------------------------
  230. //    CUngroupShapesCommand destructor
  231. //----------------------------------------------------------------------------------------
  232. CUngroupShapesCommand::~CUngroupShapesCommand()
  233. {
  234.     FW_START_DESTRUCTOR
  235.     delete fGroupList;
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    CUngroupShapesCommand::DoIt
  240. //----------------------------------------------------------------------------------------
  241. void CUngroupShapesCommand::DoIt(Environment* ev)    // Override
  242. {
  243.     if (fGroupList->Count() == 0) return;    // nothing to do
  244.  
  245.     // Remove shapes from the selection
  246.     fDrawSelection->CloseSelection(ev);
  247.  
  248.     // Iterate saved shapes and ungroup each one
  249.     CShapeCollectionIterator it(fGroupList);
  250.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  251.     {
  252.         this->UngroupShape(ev, (CGroupShape*)shape);
  253.     }
  254.  
  255.     fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. //    CUngroupShapesCommand::UndoIt
  260. //----------------------------------------------------------------------------------------
  261. void CUngroupShapesCommand::UndoIt(Environment* ev)    // Override
  262. {
  263.     if (fGroupList->Count() == 0) return;    // nothing to do
  264.  
  265.     // Remove shapes from the selection
  266.     fDrawSelection->CloseSelection(ev);
  267.  
  268.     // Iterate saved shapes and regroup each one
  269.     CShapeCollectionIterator it(fGroupList);
  270.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  271.     {
  272.         this->RegroupShape(ev, (CGroupShape*)shape);
  273.     }
  274.  
  275.     fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  276. }
  277.  
  278. //----------------------------------------------------------------------------------------
  279. //    CUngroupShapesCommand::RedoIt
  280. //----------------------------------------------------------------------------------------
  281. void CUngroupShapesCommand::RedoIt(Environment* ev)    // Override
  282. {
  283.     this->DoIt(ev);
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. //    CUngroupShapesCommand::UngroupShape
  288. //----------------------------------------------------------------------------------------
  289. void CUngroupShapesCommand::UngroupShape(Environment* ev, CGroupShape* groupShape)
  290. {
  291.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  292.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  293.  
  294.     // Remove the group shape itself from the part
  295.     CBaseShape* followingShape = partContent->GetShapeAfter(groupShape);
  296.     partContent->RemoveShape(ev, groupShape);
  297.  
  298.     // Iterate the shapes in the group and add each to the part, in the right position
  299.     CShapeCollectionIterator it(groupShape->GetShapeList());
  300.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  301.     {
  302.         partContent->AddShape(ev, shape, followingShape);
  303.         shape->SetGroupedState(NULL);
  304.         fDrawSelection->AddToSelection(ev, shape, false);
  305.     }
  306. }
  307.  
  308. //----------------------------------------------------------------------------------------
  309. //    CUngroupShapesCommand::RegroupShape
  310. //----------------------------------------------------------------------------------------
  311. void CUngroupShapesCommand::RegroupShape(Environment* ev, CGroupShape* groupShape)
  312. {
  313.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  314.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  315.  
  316.     // Remove the shapes from the part
  317.     CBaseShape* followingShape = NULL;
  318.     CShapeCollectionIterator it(groupShape->GetShapeList());
  319.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  320.     {
  321.         followingShape = partContent->GetShapeAfter(shape);
  322.         partContent->RemoveShape(ev, shape);
  323.         shape->SetGroupedState(groupShape);
  324.     }
  325.  
  326.     // Add the group shape back into the part, in the right position
  327.     partContent->AddShape(ev, groupShape, followingShape);
  328.     fDrawSelection->AddToSelection(ev, groupShape, false);
  329. }
  330.  
  331.